import * as videoServices from '../../services/video'
import {message} from 'antd';
import {sortBy} from '../../utils/sortArray'
export default {
  namespace: 'Video',
  state: {
    //侧面菜单栏
    menu: [
      {
        pri: 1,
        id: 1001,
        menuName: '热门课程',
        subMenuName: [
          {pri: 1, id: 1001, name: '彭耀凯的成才史'},
          {pri: 2, id: 1002, name: '黄蓥的压马路历程'},
          {pri: 3, id: 1003, name: '飞哥教你做鱼沫沫'},
          {pri: 4, id: 1004, name: '学姐教你找东西'}]
      }
    ],

    //系列中的基本信息、章节、班级
    allClassId:[2001],//全部系列的ID用于生成全局唯一的系列课ID
    oneClassInfo: {
      className: '',
      classBriefing: '',
      keyTeacher: '',
      classType: '',//系列类型
      classPicName: '',//系列图片的名字
      commonCash: '',//现金价格
      commonYcoin: '',//逸贝价格
      vipCash: '',//VIP现金价格
      vipYcoin: '',//VIP逸贝价格
      teacherList: null,//老师列表，必须为null,不然勾去不掉
      courseList: null,//分组信息
      classDuration: '',
    },//某一个系列的所有信息
    chapters: [
      {id: 1, chapterName: '添加或修改请先点击左侧菜单栏', chapterDuration: '添加或修改请先点击左侧菜单栏', chapterType: '添加或修改请先点击左侧菜单栏', editOrNo: true}
    ],
    classrooms: [
      {id: 1, classroomName: '添加或修改请先点击左侧菜单栏', studentNum: '添加或修改请先点击左侧菜单栏', classroomType: '添加或修改请先点击左侧菜单栏', editOrNoInClassrooms: true}
    ],


    //老师的信息
    teacherInfo: [
      {id: 1, name: '系统获取信息失败，请刷新'}],
    oneTeacherInfo: {teacherId: '', teacherName: '皇上或娘娘，麻烦您点左侧', teacherBriefing: '皇上或娘娘，麻烦您点左侧'},



    //组别排序和系列排序
    groupInfo: [
      {pri: 1, id: 1001, name: '热门课程'},
      {pri: 2, id: 1002, name: '数学'},
      {pri: 3, id: 1003, name: '英语'},
    ],
    classInfo: [],


    //轮播图
    allSlideId:[2001],//全部系列的ID用于生成全局唯一的系列课ID
    slideInfo:[{id:1001,name:'点击“关于轮播图即可获取所有轮播图”'}],
    oneSlideInfo:{slideId:1001,courseClass:{classId: ''}, slideName:'teacher-20170731201651.png'}
  },

  reducers: {

    //保存侧边栏需要用的数据，分组和系列
    saveMenuInfo(state, {payload:newData}){
      return {
        ...state,
        menu: newData
      }
    },

    //组别排序和系列排序
    //保存组别信息
    saveGroupInfo(state, {payload:newData}){
      console.log('保存', newData);
      return {
        ...state,
        groupInfo: newData
      }
    },
    //存下所有系列的信息
    saveClassInfo(state, {payload:newData}){
      return {
        ...state,
        classInfo: newData
      }
    },
    //点击下拉框，在最初获取的全分类列表中获取二级菜单
    saveOneGroupInfo(state, {payload}){
      console.log(payload);
      //获取所有的系列课程ID
      return {
        ...state,
        classInfo:payload,
      }
    },
    //保存删除后的组别或系列
    saveDataInfoAfterDelete(state, {payload:{id, mark}}){
      if (mark == "groupInfo") {
        let data = state.groupInfo;
        let newData = data.filter(item => item.id != id);
        return {
          ...state,
          groupInfo: newData
        }
      } else if (mark == "classInfo") {
        let data = state.classInfo;
        let newData = data.filter(item => item.id != id);
        return {
          ...state,
          classInfo: newData
        }
      }
    },


    //存下所有的系列ID
    saveAllClassId(state, {payload:allClassId}){
      return {
        ...state,
        allClassId
      }
    },
    //点击侧边栏后拿到相应系列的信息
    saveOneClassInfo(state, {payload}){
      console.log(payload);
      return {...state, oneClassInfo: payload}
    },


    //保存教师信息
    saveTeacherInfo(state, {payload:newData}){
      console.log('保存', newData);
      return {
        ...state,
        teacherInfo: newData
      }
    },
    saveTeacherInfoAfterDelete(state, {payload:id}){
      console.log('在前端删除某个老师', id);
      let data = state.teacherInfo.filter(item => item.id != id);
      return {
        ...state,
        teacherInfo: data
      }
    },
    saveOneTeacherInfo(state, {payload:data}){
      console.log('保存', data);
      return {
        ...state,
        oneTeacherInfo: data
      }
    },


    //保存章节信息
    saveChapterInfo(state, {payload}){
      console.log(payload);
      return {...state, chapters: payload}
    },
    //保存删除以后的数据
    saveChapterInfoAfterDelete(state, {payload:id}){
      console.log('在前端删除某个章节', id);
      let data = state.chapters.filter(item => item.id != id);
      return {
        ...state,
        chapters: data
      }
    },


    //保存班级信息
    saveClassroomInfo(state, {payload}){
      console.log(payload);
      return {...state, classrooms: payload}
    },
    //保存删除以后的数据
    saveClassroomInfoAfterDelete(state, {payload:id}){
      console.log('在前端删除某个章节', id);
      let data = state.classrooms.filter(item => item.id != id);
      return {
        ...state,
        classrooms: data
      }
    },


    //保存轮播图信息
    saveSlideInfo(state, {payload:newData}){
      console.log('保存', newData);

      let allSlideId = [];
      newData.map((item) => {
        allSlideId.push(item.id)
      });

      return {
        ...state,
        slideInfo: newData,
        allSlideId
      }
    },
    saveOneSlideInfo(state, {payload:id}){
      console.log('获取某一个轮播图',id);
      let slideInfo = state.slideInfo;
      let oneSlideInfo = slideInfo.filter(item => item.id == id )[0];
      return {
        ...state,
        oneSlideInfo
      }
    },



    //更新所有需要排序的东西，根据mark进行判定到底是什么的排序
    updateOrder(state, {payload:{newOrder, mark}}){
      console.log(newOrder, mark);
      if (mark == "groupInfo") {
        return {...state, groupInfo: newOrder}
      } else if (mark == "classInfo") {
        return {...state, classInfo: newOrder}
      }
    },

  },
  effects: {
    //一开始就获取菜单栏
    *fetchMenu({payload}, {call, put}){
      //侧边栏点击 或点击顶部第二个导航拿到初始组别列表
      let {data} = yield call(videoServices.fetchMenu);
      console.log('获取到的全部数据列表', data);
      let newData;
      let allClassId =[];
      if (data.length == 0) {
        newData = [{pri: 1, menuName: '课程待添加', subMenuName: [{pri: 1, id: 1001, name: '系列待添加'}]}]
      } else {
        newData = data.map((item) => {
          item.id = item.courseId;
          delete item.courseId;
          item.pri = item.coursePri;
          delete item.coursePri;
          item.menuName = item.courseName;
          delete item.courseName;
          item.subMenuName = item.groupClassList.map((child) => {
            child.id = child.courseClass.classId;
            child.name = child.courseClass.className;
            delete child.courseClass;
            allClassId.push(child.id);
            return child
          });
          item.subMenuName.sort(sortBy('pri', false, parseInt));
          delete item.groupClassList;
          return item
        });
      }

      newData.sort(sortBy('pri', false, parseInt));
      console.log('改变过键名并排序后的数据', newData);
      yield put({type: "saveMenuInfo", payload: newData});
      yield put({type: "saveAllClassId", payload: allClassId});

    },
    *fetchGroupInfo({payload}, {call, put}){
      //打开/video页面或点击顶部第二个导航拿到初始组别列表
      let {data} = yield call(videoServices.fetchGroupInfo);
      console.log('获取到的组别列表', data);
      let newData = data.map((item) => {
        item.id = item.courseId;
        delete item.courseId;
        item.pri = item.coursePri;
        delete item.coursePri;
        item.name = item.courseName;
        delete item.courseName;
        return item
      });
      newData.sort(sortBy('pri', false, parseInt));
      yield put({type: "saveGroupInfo", payload: newData})
    },
    //获取一个分组的所有系列
    *fetchOneGroupInfo({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.fetchOneGroupInfo,values.id);
      console.log('获取到的一个组别的所有系列列表', data);
      let newData = data.map((item) => {
        item.name = item.courseClass.className;
        delete item.courseClass;
        delete item.course;
        return item
      });

      newData.sort(sortBy('pri', false, parseInt));
      console.log(newData)
      yield put({type: "saveOneGroupInfo", payload: newData})
    },


    //获取、修改、删除老师信息
    //获取老师的所有信息
    *fetchTeacherInfo({payload}, {call, put}){
      //打开/video页面或点击顶部第三个导航拿到初始老师列表
      let {data} = yield call(videoServices.fetchTeacherInfo);
      console.log('获取到的老师列表', data);
      let newData = data.map((item) => {
        item.id = item.teacherId ? item.teacherId : 'id';
        item.name = item.teacherName ? item.teacherName + ` id:${item.teacherId}` : '请先编辑老师信息' + ` id:${item.teacherId}`;
        delete item.teacherId;
        delete item.teacherName;
        return item
      });
      newData.sort(sortBy('pri', false, parseInt));
      yield put({type: "saveTeacherInfo", payload: newData})
    },
    //获取某一个老师的详细信息:简介等
    *fetchOneTeacherInfo({payload:id}, {call, put}){
      //侧边栏点击拿到某个系列的图片信息、价格等信息
      let {data} = yield call(videoServices.fetchOneTeacherInfo, id);
      console.log('获取到的老师详情', data);

      yield put({type: "saveOneTeacherInfo", payload: data})
    },
    //修改一个老师的信息
    *modifyOneTeacherInfo({payload:values}, {call, put}){
      //打开/video页面或点击顶部第三个导航拿到初始老师列表
      console.log(values);
      let {data} = yield call(videoServices.modifyOneTeacherInfo, values);
      console.log('修改老师后台返回的消息', data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "fetchTeacherInfo"})
      } else {
        message.error(data.msg);
      }
    },
    //删除一个老师的信息
    *deleteOneTeacherInfo({payload:id}, {call, put}){
      //打开/video页面或点击顶部第三个导航拿到初始老师列表
      console.log(id);
      let {data} = yield call(videoServices.deleteOneTeacherInfo, id);
      console.log('删除老师后台返回的消息', data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "saveTeacherInfoAfterDelete", payload: id})
      } else {
        message.error(data.msg);
      }
    },


    //排序组别、系列等
    //向后端发送最新数据,组别
    *updateGroupData({payload:values}, {call, put}){
      console.log(values.mark);
      //侧边栏点击 已发货拿全部商品 ，通过状态展示退款状态下的商品，在reducer同步中filter
      let {data} = yield call(videoServices.updateGroupData, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "fetchGroupInfo"})
      } else {
        message.error(data.msg);
      }
      // yield put({type:"saveGroupInfo",payload:{data}})
    },
    //向后端发送最新数据,系列
    *updateClassData({payload:values}, {call, put}){
      console.log(values);
      //侧边栏点击 已发货拿全部商品 ，通过状态展示退款状态下的商品，在reducer同步中filter
      let {data} = yield call(videoServices.updateClassData, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
      } else {
        message.error(data.msg);
      }
      // yield put({type:"saveGroupInfo",payload:{data}})
    },
    //向后端发送请求，删除其中一条数据
    *deleteData({payload:values}, {call, put}){
      console.log(values)
      if (values.isNewOne == "yes") {//如果是新增的不用向后端请求
        yield put({type: "saveDataInfoAfterDelete", payload: values})
      } else {
        let {data} = yield call(videoServices.deleteData, values);
        console.log(data);
        if (data.result == true) {
          message.success(data.msg);
          yield put({type: "saveDataInfoAfterDelete", payload: values})
        } else {
          message.error(data.msg);
        }
      }
    },



    //获取某一个系列视频的详细信息：价格等
    *fetchOneClassInfo({payload:values}, {call, put}){
      //侧边栏点击拿到某个系列的图片信息、价格等信息
      let {data} = yield call(videoServices.fetchOneClassInfo,values);
      console.log('获取到的系列列表', data);
      //将课程分组的ID挑出，用于初始化页面
      data.courseListTem = data.courseList;
      data.teacherListTem = data.teacherList;
      data.courseList =[];
      data.teacherList =[];
      data.courseListTem.map((item) => {
        return data.courseList.push(item.courseId)
      });
      data.teacherListTem.map((item) => {
        return data.teacherList.push(item.teacherId)
      });
      delete data.courseListTem;
      delete data.teacherListTem;
      console.log(data);
      yield put({type: "saveOneClassInfo", payload: data})
    },
    //删除一个系列视频,只在当前组生效
    *deleteOneClassInfo({payload:id}, {call, put}){
      //侧边栏点击拿到某个系列的图片信息、价格等信息
      let {data} = yield call(videoServices.deleteOneClassInfo,id);
      console.log('获取到的系列列表', data);
      console.log('删除系列视频后台返回的消息', data);
      if (data.result == true) {
        message.success(data.msg);
        // yield put({type: "saveTeacherInfoAfterDelete", payload: id})
      } else {
        message.error(data.msg);
      }
      // yield put({type: "saveOneClassInfo", payload: data})
    },
    //删除一个系列视频,在所有组都生效
    *deleteOneClassInfoInAllCourse({payload:id}, {call, put}){
      //侧边栏点击拿到某个系列的图片信息、价格等信息
      let {data} = yield call(videoServices.deleteOneClassInfoInAllCourse,id);
      console.log('获取到的系列列表', data);
      console.log('删除系列视频后台返回的消息', data);
      if (data.result == true) {
        message.success(data.msg);
        // yield put({type: "saveTeacherInfoAfterDelete", payload: id})
      } else {
        message.error(data.msg);
      }
      // yield put({type: "saveOneClassInfo", payload: data})
    },
    //修改某一个系列视频的详细信息
    *modifyOneClassInfo({payload:values}, {call, put}){
      //打开/video页面或点击顶部第三个导航拿到初始老师列表
      console.log(values);
      let {data} = yield call(videoServices.modifyOneClassInfo, values);
      console.log('修改系列视频后台返回的消息', data);
      if (data.result == true) {
        message.success(data.msg);
        // yield put({type: "saveTeacherInfoAfterDelete", payload: id})
      } else {
        message.error(data.msg);
      }
    },



    //获取某一个系列的章节
    *fetchChapterInfo({payload:values}, {call, put}){
      //侧边栏点击拿到某个系列的章节信息
      console.log(values)
      let {data} = yield call(videoServices.fetchChapterInfo,values);
      console.log('获取到的章节列表', data);

      let newData = data.map((item)=>{
        item.id = item.chapterId;
        delete item.chapterId;
        item.editOrNo = true
        delete item.courseClass;
        return item
      });
      yield put({type: "saveChapterInfo", payload: newData})
    },
    //添加章节
    *addChapter({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.addChapter, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "fetchChapterInfo", payload: values.id})
      } else {
        message.error(data.msg);
      }
    },
    //更新章节
    *updateChapter({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.updateChapter, values.needToUpdateOne);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "fetchChapterInfo", payload: values.id})
      } else {
        message.error(data.msg);
      }
    },
    //删除章节
    *deleteChapter({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.deleteChapter, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        //在前端删除
        yield put({type: "saveChapterInfoAfterDelete", payload: values})
      } else {
        message.error(data.msg);
      }
    },


    //获取某一个系列的班级
    *fetchClassroomInfo({payload:values}, {call, put}){
      //侧边栏点击拿到某个系列的章节信息
      console.log(values);
      let {data} = yield call(videoServices.fetchClassroomInfo,values);
      console.log('获取到的班级列表', data);
      let newData = data.map((item)=>{
        item.classroomName = item.className;
        item.studentNum = item.num;
        item.classroomType = item.type;
        item.editOrNoInClassrooms = true
        delete item.className;
        delete item.num;
        delete item.type;
        return item
      });
      newData.sort(sortBy('id', false, parseInt));
      yield put({type: "saveClassroomInfo", payload: newData})
    },
    //添加班级
    *addClassroom({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.addClassroom, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "fetchClassroomInfo", payload: {classId:values.id,page:values.page,pageSize:values.pageSize}})
      } else {
        message.error(data.msg);
      }
    },
    //更新班级
    *updateClassroom({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.updateClassroom, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "fetchClassroomInfo", payload: {classId:values.needToUpdateOne.courseClass.classId,page:values.page,pageSize:values.pageSize}})
      } else {
        message.error(data.msg);
      }
    },
    //删除班级
    *deleteClassroom({payload:values}, {call, put}){
      console.log(values);
      let {data} = yield call(videoServices.deleteClassroom, values);
      console.log(data);
      if (data.result == true) {
        message.success(data.msg);
        //在前端删除
        yield put({type: "saveClassroomInfoAfterDelete", payload: values.id})
      } else {
        message.error(data.msg);
      }
    },



  //  获取轮播图全部信息
    *fetchSlideInfo({payload}, {call, put}){
      //打开/video页面或点击顶部第三个导航拿到初始老师列表
      let {data} = yield call(videoServices.fetchSlideInfo);
      console.log('获取到的轮播图列表', data);
      let newData = data.map((item) => {
        item.id = item.id ? item.id : 2001;
        item.name = item.carouselImgName ? item.carouselImgName : '请先上传';
        delete item.carouselImgName;
        return item
      });

      newData.sort(sortBy('id', true, parseInt));
      yield put({type: "saveSlideInfo", payload: newData})
    },
    //删除选中的轮播图
    *deleteOneSlideInfo({payload:id}, {call, put}){
      console.log(id);
      let {data} = yield call(videoServices.deleteOneSlideInfo, id);
      console.log('删除老师后台返回的消息', data);
      if (data.result == true) {
        message.success(data.msg);
        yield put({type: "saveTeacherInfoAfterDelete", payload: id})
      } else {
        message.error(data.msg);
      }
    },

  },
  subscriptions: {
    //监听到video地址下时,获取相应的组别、系列
    setup({dispatch, history}){
      return history.listen(({pathname, query}) => {
        console.log(query);
        if (pathname === '/admin') {
          dispatch({type: 'fetchMenu', payload: query})
          dispatch({type: 'fetchTeacherInfo', payload: query})
          dispatch({type: 'fetchGroupInfo', payload: query})
        }
      })
    }
  },
};







